package com.lyfx.ly.service.impl;

import com.lyfx.ly.entity.AttrEntity;
import com.lyfx.ly.service.AttrService;
import com.lyfx.ly.vo.BaseAttrs;
import com.lyfx.ly.vo.BaseValuesVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.Query;

import com.lyfx.ly.dao.ClassCourseAttrValuesDao;
import com.lyfx.ly.entity.ClassCourseAttrValuesEntity;
import com.lyfx.ly.service.ClassCourseAttrValuesService;
import org.springframework.transaction.annotation.Transactional;


@Service("classCourseAttrValuesService")
public class ClassCourseAttrValuesServiceImpl extends ServiceImpl<ClassCourseAttrValuesDao, ClassCourseAttrValuesEntity> implements ClassCourseAttrValuesService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ClassCourseAttrValuesEntity> wrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<ClassCourseAttrValuesEntity> page = this.page(
                new Query<ClassCourseAttrValuesEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveAttrsValues(BaseValuesVo baseValuesVo) {
        //1、保存attrvalue的基本信息
        List<BaseAttrs> baseAttrs = baseValuesVo.getBaseAttrs();
        List<ClassCourseAttrValuesEntity> collect = baseAttrs.stream().map(attrs -> {
            ClassCourseAttrValuesEntity entity = new ClassCourseAttrValuesEntity();
            entity.setAttrId(attrs.getAttrId());
            AttrEntity id = attrService.getById(attrs.getAttrId());
            entity.setAttrName(id.getAttrName());
            entity.setAttrValue(attrs.getAttrValues());
            entity.setQuickShow(attrs.getQuickShow() == null ? 0 : attrs.getQuickShow());
            entity.setCourseId(baseValuesVo.getCourseId());
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);

        //TODO 、保存积分相关的信息  baseValuesVo.getBounds()
        /**
         * 调用远程服务
         *  1、配置到nacos服务中心
         *  2、使用openfeign 在需要调用的地方
         *   - 写个接口  接口上面写上  @FeignClient("远程调用服务名")
         *   - 在运行的主类上加@EnableFeignClients(basePackages = "扫描的包")
         *   - 在此接口上加上@mapping（”路径xxx“）  在参数上加上@requestBody注解
         */

    }

    @Override
    public List<ClassCourseAttrValuesEntity> baseAttrForCourse(Long courseId) {
        List<ClassCourseAttrValuesEntity> course_id = this.baseMapper.selectList(new QueryWrapper<ClassCourseAttrValuesEntity>().eq("course_id", courseId));

        return course_id;
    }

    @Override
    @Transactional
    public void updateCourseAttr(Long courseId, List<ClassCourseAttrValuesEntity> entities) {
        //1|删除courseid对应的所有属性
        this.baseMapper.delete(new QueryWrapper<ClassCourseAttrValuesEntity>().eq("course_id", courseId));
        //更新插入新的值
        List<ClassCourseAttrValuesEntity> collect = entities.stream().map(item -> {
            item.setCourseId(courseId);
            return item;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}